package pathways;

/**
 * Title:        Pathways
 * Description:  Pure Java version of the Pathways Project.
 * Copyright:    Copyright (c) 2002
 * Company:      Ants Technologies
 * @author Veejay Sani
 * @version 1.0
 */

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import pathways.Applet.CAppletInterface;
import pathways.Database.Implementation.CDBUpdate;
import pathways.Database.Implementation.CDatabase;
import pathways.util.CDBUtil;
import pathways.util.CUtil;
import pathways.util.Debug.CDebug;

public class CCompound {

  public CCompound(String cmpdName, long cmpdID) {
    name = cmpdName;
    ID = cmpdID;
  }

  public String getName() { return name; }
  public void setName(String name) { this.name = name; }

  public long getID() { return ID; }
  public void setID(long ID) { this.ID = ID; }
    
  public boolean read(long cmpdID, CDatabase db)
  { 
    if (mbRead)
       return true;
       
    ResultSet r = db.getSelect().getCompound(cmpdID);
    
    try{
      if (!r.next())
         return false;
      
      ID = cmpdID;
      nameInDB = name = r.getString(1);
      keywords = r.getString(2);
      ECNO = r.getString(4);
      allNames.add(name);
      
      while (r.next())
      {
        allNames.add(r.getString(1));      
      }
      
      // free up database resources
      r.close();
      
      readAllNames = true;
      mbRead = true;  
    } catch(SQLException e) {
      ID = 0;
      name = "";
      keywords = "";
      ECNO = "";
      CDebug.println(this,"Well the rs is not empty but still read error. Exception: " + e.toString());
      
      return false;   
    }
    
    return true;
  }
  
  public void setKeywords(String keywds)
  {
    keywords = keywds;  
  }
  
  public String getKeywords()
  {
    return keywords; 
  }
  
  public void setECNO(String ecno)
  {
    ECNO = ecno; 
  }
  
  public String getECNO()
  {
    return ECNO; 
  }

  public String getECNO2()
  {
    return CUtil.formatECNO(ECNO); 
  }
  
  public boolean save(CDatabase db)
  {
    CDBUpdate update = db.getUpdate();
    
    keywords = CDBUtil.getSortVersion(name);
    
    if (ID > 0)
    {
      // update
      if (!update.UpdateCompound(name,keywords,ECNO,ID,nameInDB))
      {
        CDebug.println(this,"save() Unable to update the compound: " + name);
        return false; 
      }   
    }
    else
    {
      // new
      ID = update.newCompound(name,keywords,ECNO);
      
      if (!(ID > 0))
      {
        CDebug.println(this,"save() Unable to create the new compound: " + name);
        return false; 
      }  
    }
    
    nameInDB = name;
    
    return true;  
  }
  
  public Vector getAllNames(CDatabase db)
  {
    // returns the synonyms for this compound
    // include the name stored in the name variable
    
    if (!readAllNames)
    {
      if (ID > 0)
      {
        ResultSet r = db.getSelect().getCompound(ID);
        
        allNames.removeAllElements();
        
        try{
          while(r.next())
          {
            allNames.add(r.getString(1));  
          }
        } catch (SQLException sqle) {
          CDebug.println(this,"getAllNames() Exception: " + sqle.toString());
          allNames.removeAllElements();
          return allNames;
        }
        readAllNames = true; 
      }
    }
    
    return allNames;
  }

  // VJ???
  public InputStream getFile()
  {
    InputStream inputFile = null;
    try {
      URL codebase = CAppletInterface.getMolFileFolder();
      
      if (codebase != null)
      {
        String base = codebase.toString();
        
        URL fileURL = new URL(base + ID + ".mol");
        
        inputFile = fileURL.openStream();
      }
      else
      {
        CDebug.println(this,"getFile() -- codebase is null.");
      }
    }
    catch (MalformedURLException mue) {
      System.out.println(mue);
    }
    catch (IOException ioe) {
      System.out.println(ioe);
    }

    return inputFile;
  }
  
  public String toString()
  {
    return name;  
  }
   
  protected String name = "";
  protected long ID = 0;
  protected String keywords = "";
  protected String ECNO = "";
  protected Vector allNames = new Vector();
  protected boolean readAllNames = false;
  private boolean mbRead = false;
  private String nameInDB = "";
}